<template>
  <div></div>
</template>
<script type="text/ecmascript-6">
  export default {
    props: [],
    data(){
      common.pageTitle('微信登录')
      return {
        GWC: {
          version: '1.1.1',
          urlParams: {},
        }
      }
    },
    mounted () {
      let that = this
      //that.getUrlParams();
      that.doRedirect();
    },
    methods: {
      appendParams: function(url, params) {
//        console.log(345)
        let that = this
        if (params) {
          var baseWithSearch = url.split('#')[0];
          var hash = url.split('#')[1];
          for (var key in params) {
            var attrValue = params[key];
            if (attrValue !== undefined) {
              var newParam = key + "=" + attrValue;
              if (baseWithSearch.indexOf('?') > 0) {
                var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
                if (oldParamReg.test(baseWithSearch)) {
                  baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                } else {
                  baseWithSearch += "&" + newParam;
                }
              } else {
                baseWithSearch += "?" + newParam;
              }
            }
          }

          if (hash) {
            url = baseWithSearch + '#' + hash;
          } else {
            url = baseWithSearch;
          }
        }
        return url;
      },
      getUrlParams: function() {
//        console.log(456)
        let that = this
        var pairs = location.search.substring(1).split('&');
        for (var i = 0; i < pairs.length; i++) {
          var pos = pairs[i].indexOf('=');
          if (pos === -1) {
            continue;
          }
          that.GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
        }
      },
      doRedirect: function() {
        let that = this
        var code = that.GWC.urlParams['code'];
        var state = that.GWC.urlParams['state'];
        var redirectUri;
        // 从微信授权页面跳转回来，已经获取到了code，再次跳转到实际所需页面
        redirectUri = that.appendParams(that.GWC.urlParams['redirect_uri'], {
          'code': code,
          'state': state
        });
//        console.log(123)
//        console.log('redirectUrl', redirectUri)
//        console.log('code', code)
        //location.href = redirectUri;//此处ajax发送请求
        common.getInfoByCode(that, redirectUri, code, '/bindPhoneNo');  //根据返回信息跳到对应界面
      }
    }
  }
</script>
<style></style>